#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QTimer>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setStyleSheet("background-color: black");

    //设置初始度数
    startAngle = 150;

    //初始化计时器
    startTimer();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    //初始化画布
    initCanvas(painter);

    //画刻度
    drawScale(painter, height() / 2);

    //画指针和线
    drawPointerAndLine(painter);

    //画扇型
    drawsSector(painter, height() / 2 + 25);

    //画中心小圆 以及文字
    drawMiddleCircle(painter, 80);

    //画外圈光环
    drawEllipseOutterShine(painter, height() / 2 + 25);

    //画车标
    drawIcon(painter, height() / 2);

}

void Widget::initCanvas(QPainter& painter)
{
    //去除毛边
    painter.setRenderHint(QPainter::Antialiasing, true);

    //坐标系平移到窗口中心
    QPoint pos(rect().width() / 2, rect().height() * 0.6);
    painter.translate(pos);
}

void Widget::drawMiddleCircle(QPainter &painter, int radius)
{
    //画中心小圆旁的渐变环，即画渐变圆 然后用小圆遮挡它的中心部分
    QRadialGradient radia(0, 0, radius + 40);
    radia.setColorAt(0.0, QColor(255, 0, 0, 120));
    radia.setColorAt(0.5, QColor(255, 0, 0, 95));
    radia.setColorAt(1.0, QColor(255, 0, 0, 60));
    painter.setBrush(radia);
    painter.drawEllipse(QPoint(0, 0), radius + 40, radius + 40);

    //画中心小圆
    painter.setBrush(Qt::black);
    painter.drawEllipse(QPoint(0, 0), radius, radius);

    //设置中心里的当前值文字
    painter.setPen(QPen(Qt::white,3));
    QFont font("Arial", 30);
    font.setBold(true);//加粗字体
    painter.setFont(font);
    painter.drawText(QRect(-60, -60, 120, 70), Qt::AlignCenter, QString::number(currentValue * 4));

    //设置速度单位
    QFont font2("Arial", 13);
    font2.setBold(true);//加粗字体
    painter.setFont(font2);
    painter.drawText(QRect(-60, -60, 120, 160), Qt::AlignCenter, "Km/h");
}

void Widget::drawScale(QPainter &painter, int radius)
{
    //设置字体大小
    painter.setFont(QFont("Arial", 15));

    //让刻度线变粗
    painter.setPen(QPen(Qt::white, 5));

    //保存坐标轴方向，X轴指向三点钟方向，Y轴指向六点钟方向
    painter.save();

    //算出一个刻度需要旋转的角度
    angle = 240 * 1.0 / 60;

    //首先设置第一个刻度的角度（顺时针旋转坐标系）
    painter.rotate(startAngle);

    for(int i = 0; i <= 60; i++)
    {
        if(i >= 40)
        {
            //当度数较大时调整画笔颜色
            painter.setPen(QPen(Qt::red, 5));
        }

        if(i % 5 == 0)
        {
            //画刻度字
            painter.rotate(90);
            painter.drawText(QRect(-20, -(radius - 20), 40, 30), Qt::AlignCenter, QString::number(i * 4));
            painter.rotate(-90);

            //画长的刻度线
            painter.drawLine(radius - 20, 0, radius - 3, 0);
        }
        else
        {
            //画短的刻度线
            painter.drawLine(radius - 8, 0, radius - 3, 0);
        }
        //画完后旋转刻度线
        painter.rotate(angle);

        //度数不在较大时 又恢复到原来的颜色
        painter.setPen(QPen(Qt::white, 5));
    }
}

void Widget::drawPointerAndLine(QPainter &painter)
{
    painter.restore();//回到原点方向
    painter.save();//保存原点方向

    //画指针 通过画四边形来描出指针形状
    painter.setBrush(QBrush(Qt::white));
    painter.setPen(Qt::NoPen);
    static const QPointF points[4] ={
        QPointF(0, 0.0),  //X和Y坐标
        QPointF(200.0, -1.1),
        QPointF(200.0, 1.1),
        QPointF(0, 15.0),
    };

    painter.rotate(startAngle + angle * currentValue);
    painter.drawPolygon(points, 4);
}

void Widget::drawsSector(QPainter &painter, int radius)
{
    painter.restore();
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(255, 0, 0, 80));
    QRect rect(-radius, -radius, radius * 2, radius * 2);
    painter.drawPie(rect, (360 - startAngle) * 16, -angle * currentValue * 16);//第二个参数是初始角度，相对于X轴逆时针转的度数
    //第三个参数为旋转度数，逆时针为正，顺时针为负
}

void Widget::drawEllipseOutterShine(QPainter &painter, int radius)
{

    painter.setPen(Qt::NoPen);

    //设置渐变
    QRadialGradient radia(0, 0, radius);
    radia.setColorAt(0, QColor(0, 0, 0, 0));
    radia.setColorAt(0.9, QColor(0, 0, 0, 0));
    radia.setColorAt(0.97, QColor(255, 0, 0, 120));
    radia.setColorAt(1, QColor(255, 0, 0, 200));
    painter.setBrush(radia);

    //画圆环
    QRect rect(-radius, -radius, radius * 2, radius * 2);
    painter.drawPie(rect, (360 - 150) * 16, -angle * 61 * 16);
}

void Widget::drawIcon(QPainter &painter, int radius)
{
    painter.setRenderHint(QPainter::Antialiasing, true);
    QRect rec(-60, radius * 0.36, 120, 100);
    painter.drawPixmap(rec, QPixmap(":/icon/jeep.png"));
}

void Widget::startTimer()
{
    timer = new QTimer(this);
    currentValue = 0;
    connect(timer, &QTimer::timeout, this, [=](){
        if(flag == 0)
        {
           currentValue++;
           if(currentValue >= 61)
           {
               flag = 1;
           }
        }
        if(flag == 1)
        {
           currentValue--;
           if(currentValue <= 0)
           {
               flag = 0;
           }
        }

        update();
    });
    timer->start(25);
}

